﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.项目成果管理;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.生产计划.编排生产批;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.生产计划.生产批生产.model;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.样品管理.样品订单预览;
using DXApplicationTangche.service;

namespace DXApplicationTangche.UC.生产计划.生产批生产
{
    public partial class Form已开立 : DevExpress.XtraEditors.XtraForm
    {
        private List<DTO生产批> DTO生产批s = new List<DTO生产批>();
        private String shopId { get; set; }
        private String shopName { get; set; }
        public Form已开立()
        {
            InitializeComponent();
            this.searchLookUpEdit选择店铺.Properties.DataSource = UserPrivilegesService.getShopRightQueryFacadeByOrgId("shunqiu").shopRightQueryFacadeDTOList;
        }
        /// <summary>
        /// 套号
        /// </summary>
        /// <param name="str"></param>
        public Form已开立(String str)
        {
            InitializeComponent();
            this.searchLookUpEdit选择店铺.Properties.DataSource = UserPrivilegesService.getShopRightQueryFacadeByOrgId("shunqiu").shopRightQueryFacadeDTOList;
            this.shopId = "50";
            this.shopName = "套号";
            this.查询();
        }
        private void 查询()
        {
            //选择店铺查询该店铺下的生产批
            try
            {
                //抵用接口查询数据，清洗尺寸
                this.DTO生产批s = new 生产批生产Model().build尺寸清洗(生产批生产Service.get已开立一览(this.shopId));
                List<套号生产批DTO> 套号生产批DTOs = new List<套号生产批DTO>();
                foreach (DTO生产批 item in this.DTO生产批s)
                {
                    套号生产批DTOs.Add(new 套号生产批DTO(item));
                }
                this.gridControl生产批.DataSource = 套号生产批DTOs;
                this.gridView生产批.RefreshData();
            }
            catch
            {
                MessageBox.Show("无开立信息");
            }
        }

        #region 选择店铺
        private void searchLookUpEdit选择店铺_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择店铺View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择店铺.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.shopId = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopId").ToString();
                    this.shopName = this.searchLookUpEdit选择店铺.Properties.View.GetRowCellValue(rowHandle, "shopName").ToString();
                }
            }
            if ("50".Equals(this.shopId))
            {
                this.gridColumn订单类型.Visible = true;
                this.gridColumn订单类型.VisibleIndex = 0;
            }
            else
            {
                this.gridColumn订单类型.Visible = false;
            }
            this.查询();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择店铺.ToolTip = null;
            searchLookUpEdit选择店铺.EditValue = null;
        }
        private void searchLookUpEdit选择店铺_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.shopName;
        }
        #endregion

        #region 右键功能
        private void 拆批ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Form拆批(
                FunctionHelper.GetgridViewValue(
                    this.gridView生产批,
                    this.gridView生产批.FocusedRowHandle,
                    "batchNo"),
                FunctionHelper.GetgridViewValue(
                    this.gridView生产批,
                    this.gridView生产批.FocusedRowHandle,
                    "batchQuantiry"
                        )
                ).ShowDialog();
            //重新查询
            this.查询();
            //Form拆批 form = new Form拆批(
            //    FunctionHelper.GetgridViewValue(
            //        this.gridView生产批,
            //        this.gridView生产批.FocusedRowHandle,
            //        "batchNo"),
            //    FunctionHelper.GetgridViewValue(
            //        this.gridView生产批,
            //        this.gridView生产批.FocusedRowHandle,
            //        "batchQuantiry"
            //            )
            //    );
            //form.ShowDialog();
            //if (form.List拆批.Count > 0)
            //{
            //    this.DTO生产批s.Remove(this.DTO生产批s[this.gridView生产批.FocusedRowHandle]);
            //    this.DTO生产批s.AddRange(form.List拆批);
            //}
            //this.gridControl生产批.DataSource = this.DTO生产批s;
            //this.gridView生产批.RefreshData();
        }
        private void 加急ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DTO生产批 item in this.DTO生产批s)
            {
                if (item.batchNo.Equals(FunctionHelper.GetgridViewValue(this.gridView生产批,this.gridView生产批.FocusedRowHandle,"batchNo")))
                {
                    try
                    {
                        item.urgentFlg =
                            编排生产批Service.post变更为加急状态(
                                FunctionHelper.GetgridViewValue(
                                    this.gridView生产批,
                                    this.gridView生产批.FocusedRowHandle,
                                    "batchNo"
                                    )
                                ).urgentFlg;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                }
            }
            this.gridView生产批.RefreshData();
        }

        private void 不加急ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DTO生产批 item in this.DTO生产批s)
            {
                if (item.batchNo.Equals(FunctionHelper.GetgridViewValue(this.gridView生产批, this.gridView生产批.FocusedRowHandle, "batchNo")))
                {
                    try
                    {
                        item.urgentFlg =
                            编排生产批Service.post变更为不加急状态(
                                FunctionHelper.GetgridViewValue(
                                    this.gridView生产批,
                                    this.gridView生产批.FocusedRowHandle,
                                    "batchNo"
                                    )
                                ).urgentFlg;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                }
            }
            this.gridView生产批.RefreshData();
        }
        private void 编排生产计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String batchNo = FunctionHelper.GetgridViewValue(
                    this.gridView生产批,
                    this.gridView生产批.FocusedRowHandle,
                    "batchNo");
                DTO生产批 dTO生产批 = 编排生产批Service.getWIPBatchByBatchNo(batchNo);
                if (String.IsNullOrEmpty(dTO生产批.orderId))
                {
                    return;
                }
                if ("48".Equals(this.shopId))
                {
                    new Form样品订单预览新(dTO生产批).ShowDialog();
                }
                else
                {
                    new Form订单预览新(dTO生产批).ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //重新查询
            this.查询();
        }
        #endregion

        private void gridView生产批_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //gridColumn订单类型
            if (e.Column.Name.Equals("gridColumn订单类型"))
            {
                if ("客户套号".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.OrangeRed;
                }
                if ("门店补货".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.LightYellow;
                }
                if ("其他".Equals(e.CellValue.ToString()))
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }
    }
}